﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Models.Admin;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.Admin
{
    public class GroupNhomCoBanDAO : SqlDAO
    {
        public GroupNhomCoBanDAO(string connectionString, Type type = null)
            : base(connectionString, type) { }

        public List<GroupNhomCoBan> GetByGroupId(int groupId)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spGroupNhomCoBan_GetByGroupId");
                cmd.Parameters.Add(CreateParameter("@GroupId", groupId));

                return GetList<GroupNhomCoBan>(cmd);
            }
            catch
            {
                throw;
            }
        }

        public bool Save(List<GroupNhomCoBan> data)
        {
            DataTable dataTable = GetDataTable(data);
            SqlCommand cmd = this.GetSprocCommand("spGroupNhomCoBan_InsertUpdate");
            cmd.Parameters.Add(new SqlParameter("@GroupNhomCoBanTable", dataTable));

            int result = ExecuteNonQuery(cmd);

            return result > 0 ? true : false;
        }

        private DataTable GetDataTable(List<GroupNhomCoBan> data)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID");
            table.Columns.Add("GroupId");
            table.Columns.Add("NhomCoBanId");

            for(int i = 0;i< data.Count;i++)
            {
                table.Rows.Add(data[i].ID, data[i].GroupId, data[i].NhomCoBanId);
            }

            return table;
        }
    }
}
